﻿Imports System.Data.SqlClient

Public Class frmProspectsRegistry

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Dim a As New frmProspectsFileNew
        a.ShowDialog()

        FillProspects()
    End Sub

    Private Sub FillProspects()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From Prospects Where Name Like '%' + @Name + '%' and UserName=N'" & CurrentUser & "' and Branch=N'" & Branch & "'", cnn)
            Dim reader As SqlDataReader

            GridProspects.Rows.Clear()

            cnn.Open()
            cmd.Parameters.AddWithValue("@Name", Me.txtProspectNameSearch.Text.Trim)
            reader = cmd.ExecuteReader
            While reader.Read
                Me.GridProspects.Rows.Add(reader.Item("SNo"), reader.Item("Name"), reader.Item("Address"), reader.Item("Tele"), reader.Item("CoRegDetails"), _
                                           "Edit", "Contacts Persons", reader.Item("UserName"), CDate(reader.Item("TransDate")).ToString("yyyy/MM/dd hh:mm tt"))
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmProspectsRegistry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized

    End Sub

    Private Sub GridClients_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridProspects.CellClick
        If e.ColumnIndex = Me.DGProsEdit.Index Then

            Dim a As New frmProspectsFileEdit
            a.txtProspID.Text = Me.GridProspects.CurrentRow.Cells(0).Value

            a.FillProspectDetails()
            a.FillDocuments()
            a.ShowDialog()

            FillProspects()

        ElseIf e.ColumnIndex = Me.DGProsContacts.Index Then
            Dim a As New frmProspectContacts
            a.SNo = Me.GridProspects.CurrentRow.Cells(0).Value

            a.ShowDialog()
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        FillProspects()
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

End Class